package com.tjakus.quartz;

import java.sql.Timestamp;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.tjakus.hibernate.HibernateUtil;
import com.tjakus.logging.Logging;
import com.tjakus.properties.Property;

/**
 * This class represents quartz job which is responsible for deleting old
 * advertisement data.
 * 
 * @author tjakus
 * 
 */

public class JobDBCleanup implements Job {

	/**
	 * This method executes quartz job which is responsible for deleting old
	 * advertisement data.
	 * 
	 */
	public void execute(JobExecutionContext context) throws JobExecutionException {
		Logging.getLogger().info("JobDBCleanup - execute - [ STARTED ]");
		// get latest time stamp from database
		long miliseconds = 1000;
		Timestamp timestamp = HibernateUtil.getLatestTimestamp();
		Logging.getLogger().debug(
				"JobDBCleanup - execute - getLatestTimestamp: [ " + timestamp + " ]");
		// time stamp minus two months
		timestamp.setTime(timestamp.getTime()
				- (Property.CLEANUP_PERIOD_IN_SEC * miliseconds));
		Logging.getLogger().debug(
				"JobDBCleanup - execute - deleteOlderThan: [ " + timestamp + " ]");
		// delete all records older than time stamp from advertisement table
		HibernateUtil.deleteAllOldRecordsFromAdvertisement(timestamp);
		Logging.getLogger().info("JobDBCleanup - execute - [ FINISHED ]");
	}
}
